package com.heima.admin.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.mapper.Sensitivemapper;
import com.heima.admin.service.AdSensitiveService;
import com.heima.common.exception.CustException;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.admin.dtos.SensitiveDTO;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@Slf4j
public class AdSensitiveServiceImpl extends ServiceImpl<Sensitivemapper, AdSensitive>implements AdSensitiveService {
    @Override
    public ResponseResult list(SensitiveDTO dto) {
//参数的校验
        dto.checkParam();
        Page<AdSensitive> objectPage = new Page<>(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<AdSensitive> wrapper = Wrappers.lambdaQuery();
        if (StringUtils.isNoneBlank(dto.getName())){
            wrapper.like(AdSensitive::getSensitives,dto.getName());
        }
        wrapper.orderByDesc(AdSensitive::getCreatedTime);
        IPage<AdSensitive> page = this.page(objectPage, wrapper);
        ResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(), page.getTotal());
        pageResponseResult.setData(page.getRecords());
        return pageResponseResult;
    }

    @Override
    public ResponseResult insert(AdSensitive adSensitive) {
        //1.检查参数，是否为空
        String sensitives = adSensitive.getSensitives();
    if (adSensitive == null && StringUtils.isNotBlank(sensitives)) {

      return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
    //2.判断是否已经存在数据库
        LambdaQueryWrapper<AdSensitive> eq = Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getSensitives, adSensitive.getSensitives());
        int count = this.count(eq);
        if (count>0){
            CustException.cust(AppHttpCodeEnum.DATA_EXIST,"敏感词已经存在");
        }
        //插入到数据中中
        adSensitive.setCreatedTime(new Date());
        save(adSensitive);
        //返回响应结果
        return ResponseResult.okResult();


    }

    @Override
    public ResponseResult update(AdSensitive adSensitive) {
//1.检查参数
        if (adSensitive.getId()==null){
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST,"不存在");
        }
        LambdaQueryWrapper<AdSensitive> eq = Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getSensitives, adSensitive.getSensitives());
        int count = this.count(eq);
        if (count>0){
            CustException.cust(AppHttpCodeEnum.DATA_EXIST,"敏感词已经存在");
        }

        boolean update = this.updateById(adSensitive);
    System.out.println("update = " + update);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);

    }

    @Override
    public ResponseResult delete(Integer id) {

//1.检查参数
        if(id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.查询敏感词是否存在
        AdSensitive adSensitive = getById(id);
        if(adSensitive == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //3.删除
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);


    }
    @Autowired
    AdSensitiveMapper adSensitiveMapper;
    @Override
    public ResponseResult selectAllSensitives() {
        return ResponseResult.okResult(adSensitiveMapper.findAllSensitives());
    }
}
